home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / HtmlPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  4.1 KB  |  144 lines

  1. /*
  2.  * @(#)HtmlPanel.java    1.14 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.event.*;
  17. import javax.swing.text.*;
  18. import javax.accessibility.*;
  19.  
  20. import java.awt.*;
  21. import java.net.URL;
  22. import java.net.MalformedURLException;
  23. import java.io.IOException;
  24.  
  25. /*
  26.  * @version 1.14 98/08/26
  27.  * @author Jeff Dinkins
  28.  * @author Tim Prinzing
  29.  * @author Peter Korn (accessibility support)
  30.  */
  31. public class HtmlPanel extends JPanel implements HyperlinkListener {
  32.     SwingSet swing;
  33.     JEditorPane html;
  34.  
  35.     public HtmlPanel(SwingSet swing) {
  36.     this.swing = swing;
  37.     setBorder(swing.emptyBorder10);
  38.         setLayout(new BorderLayout());
  39.     getAccessibleContext().setAccessibleName("HTML panel");
  40.     getAccessibleContext().setAccessibleDescription("A panel for viewing HTML documents, and following their links");
  41.     
  42.     try {
  43.            URL url = null;
  44.             String prefix = "file:" +
  45.                    System.getProperty("user.dir") +
  46.                    System.getProperty("file.separator");
  47.             try {
  48.                 url = new URL(prefix + "example.html");
  49.             } catch (java.net.MalformedURLException exc) {
  50.                    System.err.println("Attempted to open example.html "
  51.                                       + "with a bad URL: " + url);
  52.                    url = null;
  53.             }
  54.             
  55.             if(url != null) {
  56.                 html = new JEditorPane(url);
  57.                 html.setEditable(false);
  58.                 html.addHyperlinkListener(this);
  59.                 JScrollPane scroller = new JScrollPane();
  60.                 scroller.setBorder(swing.loweredBorder);
  61.                 JViewport vp = scroller.getViewport();
  62.                 vp.add(html);
  63.                 vp.setBackingStoreEnabled(true);
  64.                 add(scroller, BorderLayout.CENTER);
  65.             }
  66.     } catch (MalformedURLException e) {
  67.         System.out.println("Malformed URL: " + e);
  68.     } catch (IOException e) {
  69.         System.out.println("IOException: " + e);
  70.     }
  71.     }
  72.  
  73.     /**
  74.      * Notification of a change relative to a 
  75.      * hyperlink.
  76.      */
  77.     public void hyperlinkUpdate(HyperlinkEvent e) {
  78.     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  79.         linkActivated(e.getURL());
  80.     }
  81.     }
  82.  
  83.     /**
  84.      * Follows the reference in an
  85.      * link.  The given url is the requested reference.
  86.      * By default this calls <a href="#setPage">setPage</a>,
  87.      * and if an exception is thrown the original previous
  88.      * document is restored and a beep sounded.  If an 
  89.      * attempt was made to follow a link, but it represented
  90.      * a malformed url, this method will be called with a
  91.      * null argument.
  92.      *
  93.      * @param u the URL to follow
  94.      */
  95.     protected void linkActivated(URL u) {
  96.     Cursor c = html.getCursor();
  97.     Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
  98.     html.setCursor(waitCursor);
  99.     SwingUtilities.invokeLater(new PageLoader(u, c));
  100.     }
  101.  
  102.     /**
  103.      * temporary class that loads synchronously (although
  104.      * later than the request so that a cursor change
  105.      * can be done).
  106.      */
  107.     class PageLoader implements Runnable {
  108.     
  109.     PageLoader(URL u, Cursor c) {
  110.         url = u;
  111.         cursor = c;
  112.     }
  113.  
  114.         public void run() {
  115.         if (url == null) {
  116.         // restore the original cursor
  117.         html.setCursor(cursor);
  118.  
  119.         // PENDING(prinz) remove this hack when 
  120.         // automatic validation is activated.
  121.         Container parent = html.getParent();
  122.         parent.repaint();
  123.         } else {
  124.         Document doc = html.getDocument();
  125.         try {
  126.             html.setPage(url);
  127.         } catch (IOException ioe) {
  128.             html.setDocument(doc);
  129.             getToolkit().beep();
  130.         } finally {
  131.             // schedule the cursor to revert after
  132.             // the paint has happended.
  133.             url = null;
  134.             SwingUtilities.invokeLater(this);
  135.         }
  136.         }
  137.     }
  138.  
  139.     URL url;
  140.     Cursor cursor;
  141.     }
  142.  
  143. }
  144.